package com.joyance.springmvc.testhttp;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.joyance.springmvc.util.json.JsonUtils;
import com.joyance.springmvc.vo.Person;

@Controller
@RequestMapping("/testHttp")
public class TestHttpAction {

    @RequestMapping(value = "/testGet", method = RequestMethod.GET)
    public @ResponseBody String testGet(Person person, HttpServletResponse response) {
        System.out.println("============");
        // response.addHeader("Cache-Control", "max-age=20");
        return JsonUtils.writeObject(person);
    }


    @RequestMapping(value = "/testPost", method = RequestMethod.POST)
    public @ResponseBody String testPost(Person person) {
        return JsonUtils.writeObject(person);
    }


    @RequestMapping(value = "/testHead", method = RequestMethod.HEAD)
    public @ResponseBody String testHead(Person person, HttpServletResponse response) {
        response.addHeader("person_id", person.getId() + "");
        response.addHeader("person_name", person.getName());
        response.addHeader("person_age", person.getAge() + "");
        return JsonUtils.writeObject(person);
    }


    @RequestMapping(value = "/testPut", method = RequestMethod.PUT)
    public @ResponseBody String testPut(Person person, HttpServletResponse response) {
        response.addHeader("person_id", person.getId() + "");
        response.addHeader("person_name", person.getName());
        response.addHeader("person_age", person.getAge() + "");
        return JsonUtils.writeObject(person);
    }


    @RequestMapping(value = "/redirect")
    public ModelAndView redirect(String name, HttpServletResponse response) {
        response.addHeader("Cache-Control", "max-age=10");
        // response.setHeader("Cache-Control", "no-cache");
        System.out.println("===========");
        return new ModelAndView("redirect:/views/testhttp/a.html");
    }


    // @RequestMapping(value = "/goback")
    // public ModelAndView goback(String name, HttpServletResponse response) {
    // response.addHeader("Cache-Control", "no-cache/no-store");
    // System.out.println("+++++++++");
    // return new ModelAndView("redirect:/testHttp/redirect.do");
    // }

    @RequestMapping(value = "/testPostJsonArray", method = RequestMethod.POST)
    public @ResponseBody String testPostJsonArray(String data) {
        // List<LinkedHashMap<String, Object>> list = JsonUtils.readObject(data, List.class);
        // List<Person> l1 = new ArrayList<Person>();
        // for (LinkedHashMap p : list) {
        // Person x = JsonUtils.get
        // l1.add(x);
        // }
        List<Person> list = JsonUtils.getDTOList(data, Person.class);
        for (Person p : list) {
            System.out.println(p);
        }
        return data;
    }


    public static void main(String[] args) {
        BASE64Encoder encoder = new BASE64Encoder();
        String ret = encoder.encode("joy:123456".getBytes());
        System.out.println(ret);
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] bs = decoder.decodeBuffer(ret);
            System.out.println(new String(bs));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
